package cn.jarlen.jwt.tools.http;

import android.util.Log;

import org.apache.commons.net.telnet.TelnetClient;

import java.io.IOException;

/**
 * @author jarlen
 * Create by 2023/5/29 19:06
 */
public class TelnetManager {

    private static final String TAG = TelnetManager.class.getSimpleName();

    TelnetClient telnet;

    private TelnetManager() {
        telnet = new TelnetClient();
    }

    public static TelnetManager getInstance() {
        return TelnetManagerHolder.telnetManager;
    }

    private static class TelnetManagerHolder {
        public static TelnetManager telnetManager = new TelnetManager();
    }

    public boolean connect(String ip, int port, int connectTimeoutSeconds) {
        boolean result = false;
        try {
            connectTimeoutSeconds = connectTimeoutSeconds < 1 ? 1 : connectTimeoutSeconds;
            telnet.setConnectTimeout(connectTimeoutSeconds * 1000);
            telnet.connect(ip, port);
            Log.i(TAG, "the ip :" + ip + ":" + port + " telnet is successful");
            result = true;
        } catch (Exception e) {
            Log.i(TAG, "the ip :" + ip + ":" + port + " telnet is failed");
            e.printStackTrace();
        } finally {
            try {
                telnet.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
